package com.bilibili.service.impl;

import com.bilibili.entity.*;
import com.bilibili.exception.BusinessException;
import com.bilibili.mapper.PersonalMapper;
import com.bilibili.mapper.UserMapper;
import com.bilibili.service.PersonalService;
import com.bilibili.utils.RedisOperations;
import com.bilibili.vo.CollectionVo;
import com.bilibili.vo.HistoryVo;
import com.bilibili.vo.PersonalData;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.bilibili.constant.BusinessConstant.REDIS_HISTORTY_START_INDEX;
import static com.bilibili.constant.BusinessConstant.REDIS_USER_HISTORY_KEY_PREFIX;

@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    PersonalMapper personalMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private RedisOperations redisOperations;


    @Override
    public PersonalData getPersonalByuserId(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
       return personalMapper.getPersonalByuserId(userId);

    }


    @Override
    public UserInfo getUserInfoByuserId(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        return personalMapper.getUserInfoByuserId(userId);
    }


    @Override
    public boolean addUserInfo(UserInfo userInfo) {
        String name = userInfo.getName();
        User userByUserName = userMapper.getUserByUserName(name);
        if (Objects.isNull(userByUserName)){
            boolean b = personalMapper.addUserInfo(userInfo);
            if (!b){
                throw new BusinessException(HttpStatus.NOT_FOUND, "添加用户详细信息失败");
            }
        }else {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "用户名已存在");
        }
        return false;
    }

    @Override
    public boolean modifyUserInfo(UserInfo userInfo, Integer userId) {
        String name = userInfo.getName();
        User userByUserName = userMapper.getUserByUserName(name);
        if (Objects.isNull(userByUserName)){
            boolean b = personalMapper.modifyUserInfo(userInfo,userId);
            if (!b){
                throw new BusinessException(HttpStatus.NOT_FOUND, "修改用户详细信息失败");
            }
        }else {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "用户名已存在");
        }
        return false;
    }

    @Override
    public boolean addVipPoints(Integer userId, Integer balance) {
        if(Objects.isNull(userId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"用户id不能为空");
        }
        if(Objects.isNull(balance)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"用户经验值不能为空");
        }
        return personalMapper.addVipPoints(userId,balance);
    }


    @Override
    public boolean addExp(Integer userId,Integer exp) {
        if(Objects.isNull(userId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"用户id不能为空");
        }
        if(Objects.isNull(exp)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"用户经验值不能为空");
        }
        return personalMapper.addExp(userId,exp);
    }

    @Override
    public boolean addCoin(Integer userId, Integer coin) {
        if(Objects.isNull(userId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"用户id不能为空");
        }
        if(Objects.isNull(coin)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"硬币不能为空");
        }
        return personalMapper.addCoin(userId,coin );
    }


    @Override
    public Space getSpaceByuserId(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        return personalMapper.getSpaceByuserId(userId);
    }

    @Override
    public List<Video> getHistoryVideos(Integer[] tempHistoryVedioVids, User loginUser) {
        List<Integer> vids;
        if (Objects.isNull(loginUser)) {
            vids = Arrays.asList(tempHistoryVedioVids);
        } else {
            String historyKey = REDIS_USER_HISTORY_KEY_PREFIX + loginUser.getId();
            long stop = redisOperations.llen(historyKey) - 1;
            List<Integer> list = new ArrayList<>();
            for (String vidStr : redisOperations.lrange(historyKey, REDIS_HISTORTY_START_INDEX, stop)) {
                Integer parseInt = Integer.parseInt(vidStr);
                list.add(parseInt);
            }
            vids = list;
        }
        if (CollectionUtils.isEmpty(vids)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "暂无任何历史记录！");
        } else {
            return personalMapper.getVideosByIds(vids);
        }
    }

    @Override
    public List<MyCoin> getAllCoinRecord(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        return personalMapper.getAllCoinRecord(userId);
    }

    @Override
    public boolean isAchieveWatchAward(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        return personalMapper.isExistTodayWatch(userId);

    }

    @Override
    public int getCoinAward(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        int todayCoinFrequency = personalMapper.getTodayCoinFrequency(userId);
        int  coinExp = todayCoinFrequency*10;
        return coinExp;
    }

    @Override
    public List<CollectionVo> getCollectionRecord(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        return personalMapper.getCollectionRecord(userId);
    }

    @Override
    public List<HistoryVo> getHistoryRecord(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "用户id不存在");
        }
        return personalMapper.getHistoryRecord(userId);
    }


}
